from flask import Flask, url_for, request, render_template, jsonify
import mysql.connector
import uuid

app = Flask(__name__)

# 数据库

conn = mysql.connector.connect(user='root', password='d9feae382a', database='UserTrack')

# 服务器
@app.route('/Hello/')
@app.route('/Hello/<name>')
def index(name=None):
    return jsonify({'msg':'Welcome to GaoXX'})

@app.route('/location/',methods=['POST'])
def locationPost():
    id = str(uuid.uuid1().__str__())
    lat = float(request.form['lat'])
    lng = float(request.form['lng'])
    tamp = float(request.form['tamp'])
    udid = str(request.form['udid'])
    speed = float(request.form['speed'])
    acc = float(request.form['acc'])
    cursor = conn.cursor()
    cursor.execute('insert into location (id,udid, timetamp, lat, lng, speed, acc) values (%s, %s, %s, %s, %s, %s, %s)', [id, udid, tamp, lat, lng, speed, acc])
    conn.commit()
    cursor.close()
    return  jsonify({'msg':'成功'})

@app.route('/location/',methods=['GET'])
def locationGet():
    print('GETGET')
    lasttime = float(request.args['timetamp'])
    print(lasttime)
    cursor = conn.cursor()
    cursor.execute('select * from location where (timetamp > %s and acc <= 30) order by timetamp', [lasttime])
    values = cursor.fetchall()
    return jsonify({'msg':'成功','data':values})

if __name__ == '__main__':
    # 监听指定的IP 端口 打开调试模式
    app.run(host='123.56.132.3',port=9000,debug=False)

